package licode.unisop.platform.adapter.web;

import licode.unisop.platform.application.executor.SiPlatformQueryExecutor;
import licode.unisop.provider.info.*;
import licode.unisop.provider.vo.*;
import licode.unisop.unilite.result.UniResult;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 平台对外接口
 *
 * @author licode
 */
@Slf4j
@RestController
@UniReturnResult
@RequestMapping("/plat-api/v1/internal/platform")
public class PlatformQueryAdapter {
    @Resource
    private SiPlatformQueryExecutor platformQueryExecutor;

    @PostMapping("/query/lookup")
    public UniResult<SiPlatform> findPlatform(@RequestBody SiPlatIdFind findInfo) {
        return UniResult.of(platformQueryExecutor.lookupPlatform(findInfo));
    }

    /**
     * 获取用户、角色、客户等资源信息
     * @param idFind 领域和资源ID
     * @return 用户资源
     */
    @PostMapping("/asset/query/info")
    public UniResult<SiAssetEntry> findAsset(@RequestBody SiAssetId idFind) {
        return UniResult.of(platformQueryExecutor.lookupAssetInfo(idFind));
    }

    @PostMapping("/asset/query/brief-info")
    public UniResult<SiAssetBrief> lookupAssetBrief(@RequestBody SiAssetLookupIn lookupIn) {
        return UniResult.of(platformQueryExecutor.lookupAssetBrief(lookupIn));
    }

    @PostMapping("/asset/query/sub-count")
    public UniResult<Long> getSubAssetCount(@RequestBody SiAssetLookupIn lookupIn) {
        return UniResult.of(platformQueryExecutor.getSubAssetCount(lookupIn));
    }

    @PostMapping("/asset/query/asset-id/keys")
    public UniResult<List<SiAssetKey>> findAssetKeys(@RequestBody SiAssetId idFind) {
        return UniResult.of(platformQueryExecutor.findAssetKeys(idFind));
    }

    /**
     * 获取平台资源信息
     * @param plInfo 平台ID查询条件
     * @return 平台资源信息
     */
    @PostMapping("/asset/query/platform-assets/info")
    public UniResult<SiAssetInfo> getPlatformAssets(@RequestBody SiPlatIdFind plInfo) {
        return UniResult.of(platformQueryExecutor.getPlatformAssets(plInfo));
    }

    @PostMapping("/asset/query/lookup-auth-asset/by-code")
    public UniResult<SiAssetBrief> lookupAuthAssetByCode(@RequestBody SiAssetCodeLookupIn req) {
        return UniResult.of(platformQueryExecutor.lookupAuthAssetByCode(req));
    }

    @PostMapping("/asset/query/lookup-asset/by-code")
    public UniResult<SiAssetBrief> lookupAssetByCode(@RequestBody SiLookupAssetByCodeIn req) {
        return UniResult.of(platformQueryExecutor.lookupAssetByCode(req));
    }

    @PostMapping("/asset/query/asset-list/by-code")
    public UniResult<List<SiAssetBrief>> lookupAssetByCode(@RequestBody SiAssetCodeQueryIn req) {
        return UniResult.of(platformQueryExecutor.queryAssetsByCode(req));
    }

    @PostMapping("/asset/query/user-assets/info")
    public UniResult<SiAssetHold> getUserAssets(@RequestBody SiHoldInfo holdInfo) {
        return UniResult.of(platformQueryExecutor.getUserAssets(holdInfo));
    }

    @PostMapping("/asset/query/hold-assets/info")
    public UniResult<SiAssetHold> getHoldAssets(@RequestBody SiHoldInfo holdInfo) {
        return UniResult.of(platformQueryExecutor.getHoldAssets(holdInfo));
    }

    /**
     * 分页查找用户信息
     * @param req 查找条件
     * @return 用户信息
     */
    @PostMapping("/query/page")
    public UniResult<SiPageResult<SiPlatform>> findPage(@RequestBody PlatPageInfo req) {
        return UniResult.of(platformQueryExecutor.findPlatformPage(req));
    }

    @PostMapping("/asset/query/page")
    public UniResult<SiPageResult<SiAssetBrief>> findAssetPage(@RequestBody AssetPageInfo req) {
        return UniResult.of(platformQueryExecutor.findAssetPage(req));
    }

    @PostMapping("/auth/query/page")
    public UniResult<SiPageResult<String>> findAuthPlatformPage(@RequestBody AuthPlatPageInfo req) {
        return UniResult.of(platformQueryExecutor.findAuthPlatformPage(req));
    }

}
